package com.sky.controller.user;

import cn.dev33.satoken.annotation.SaCheckLogin;
import cn.mybatis.mp.core.mybatis.mapper.context.Pager;
import com.sky.dto.AuthorBooksDTO;
import com.sky.dto.BookFilterDTO;
import com.sky.dto.BookSearchDTO;
import com.sky.dto.HotBooksDTO;
import com.sky.entity.Categories;
import com.sky.result.Result;
import com.sky.service.BookService;
import com.sky.service.CategoriesService;
import com.sky.vo.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 用户图书相关接口
 */
@Slf4j
@RestController
@RequestMapping("/user")
public class UserBookController {

    @Autowired
    private BookService bookService;

    @Autowired
    private CategoriesService categoriesService;

    /**
     * 获取图书详细信息
     *
     * @param bookId 图书ID
     * @return BooksVO
     */
    @SaCheckLogin
    @GetMapping("/getBooksDetails/{bookId}")
    public Result<BooksVO> getBooksDetails(@PathVariable Integer bookId) {
        log.info("获取图书详细信息");
        BooksVO booksVOList = bookService.getBooksDetails(bookId);
        return Result.success(booksVOList);
    }

    /**
     * 获取全部图书分类
     * @return 图书分类列表
     */
    @SaCheckLogin
    @GetMapping("/categories")
    public Result<List<Categories>> getAllCategories() {

        log.info("获取全部图书分类");

        List<Categories> categories = categoriesService.getCategories();

        return Result.success(categories);

    }

    /**
     * 根据分类和字数条件分页查询图书
     *
     * @param bookFilterDTO 查询条件（分类ID、字数范围、排序方式等）
     * @return 分页的图书信息
     */
    @SaCheckLogin
    @PostMapping("/books/filter")
    public Result<Pager<BookFilterVO>> getBooksByFilter(@RequestBody BookFilterDTO bookFilterDTO) {
        log.info("根据条件筛选图书：{}", bookFilterDTO);
        Pager<BookFilterVO> pager = bookService.getBooksByFilter(bookFilterDTO);
        return Result.success(pager);
    }

    /**
     * 搜索图书
     *
     * @param keyword 搜索关键词（图书名）
     * @param pageNumber 页码（可选，默认1）
     * @param pageSize 每页大小（可选，默认10）
     * @return 分页的图书信息
     */
    @SaCheckLogin
    @GetMapping("/books/search")
    public Result<Pager<BookSearchVO>> searchBooks(
            @RequestParam String keyword,
            @RequestParam(required = false, defaultValue = "1") Integer pageNumber,
            @RequestParam(required = false, defaultValue = "10") Integer pageSize) {

        log.info("搜索图书，关键词：{}，页码：{}，每页大小：{}", keyword, pageNumber, pageSize);

        BookSearchDTO bookSearchDTO = new BookSearchDTO();
        bookSearchDTO.setKeyword(keyword);
        bookSearchDTO.setPageNumber(pageNumber);
        bookSearchDTO.setPageSize(pageSize);

        Pager<BookSearchVO> pager = bookService.searchBooks(bookSearchDTO);
        return Result.success(pager);
    }

    /**
     * 获取作者的所有图书
     *
     * @param author 作者名
     * @param pageNumber 页码（可选，默认1）
     * @param pageSize 每页大小（可选，默认20）
     * @return 作者图书分页信息
     */
    @SaCheckLogin
    @GetMapping("/books/author")
    public Result<AuthorBooksPager> getAuthorBooks(
            @RequestParam String author,
            @RequestParam(required = false, defaultValue = "1") Integer pageNumber,
            @RequestParam(required = false, defaultValue = "20") Integer pageSize) {

        log.info("获取作者图书，作者：{}，页码：{}，每页大小：{}", author, pageNumber, pageSize);

        AuthorBooksDTO authorBooksDTO = new AuthorBooksDTO();
        authorBooksDTO.setAuthor(author);
        authorBooksDTO.setPageNumber(pageNumber);
        authorBooksDTO.setPageSize(pageSize);

        AuthorBooksPager pager = bookService.getAuthorBooks(authorBooksDTO);
        return Result.success(pager);
    }

    /**
     * 获取热门书籍排行榜
     *
     * @param pageNumber 页码
     * @param pageSize 每页大小
     * @return 热门书籍分页数据
     */
    @SaCheckLogin
    @GetMapping("/books/hot")
    public Result<Pager<HotBooksVO>> getHotBooks(
            @RequestParam(required = false, defaultValue = "1") Integer pageNumber,
            @RequestParam(required = false, defaultValue = "10") Integer pageSize) {

        log.info("获取热门书籍排行榜，页码：{}，每页大小：{}", pageNumber, pageSize);

        try {
            HotBooksDTO hotBooksDTO = new HotBooksDTO();
            hotBooksDTO.setPage(pageNumber);
            hotBooksDTO.setPageSize(pageSize);

            Pager<HotBooksVO> hotBooks = bookService.getHotBooks(hotBooksDTO);
            return Result.success(hotBooks);
        } catch (Exception e) {
            log.error("获取热门书籍排行榜失败", e);
            return Result.error("获取热门书籍排行榜失败：" + e.getMessage());
        }
    }

} 